package com.example.mediaservice.controller;

import com.example.mediaservice.websocket.SendImage;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/image")
public class ReceiveImage {
    @PostMapping
    public String upload(@RequestBody byte[] imgData) throws IOException {
        // 开始时间
        long stime = System.currentTimeMillis();
        // 转发到前端
        ByteBuffer byteBuffer = ByteBuffer.wrap(imgData);
        Object[] arr = SendImage.sendImgSessions.toArray();
        for(Object _item:arr) {
            SendImage item = (SendImage) _item;
            item.sendMessage(byteBuffer);
            System.out.println("sendMessage => end : " + arr.length + " , " + byteBuffer.capacity());
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        System.out.printf("执行时长：%d 毫秒.", (etime - stime));
        return "上传成功";
    }
}
